using B.S.Data.MES.API.Applications.CangKu.Command;
using B.S.Data.MES.API.Dto.CangKuDTO;
using B.S.Domain.CangKu;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B.S.Data.MES.API.Applications.CangKu.CommandHandlers
{
    public class GetCangKuQUPagedCommandHandler : IRequestHandler<GetCangKuQUPagedCommand, APIRequst<APIPageing<List<CangKuQUWithCangKuDTO>>>>
    {
        private readonly IBaseRepository<CangKuQU> _cangKuQURepository;
        private readonly IBaseRepository<CangKuinfo> _cangKuRepository;

        public GetCangKuQUPagedCommandHandler(
            IBaseRepository<CangKuQU> cangKuQURepository,
            IBaseRepository<CangKuinfo> cangKuRepository)
        {
            _cangKuQURepository = cangKuQURepository;
            _cangKuRepository = cangKuRepository;
        }

        public Task<APIRequst<APIPageing<List<CangKuQUWithCangKuDTO>>>> Handle(GetCangKuQUPagedCommand request, CancellationToken cancellationToken)
        {
            
            
                // 使用内连接查询CangKuQU和CangKuinfo
                var resultQuery = from qu in _cangKuQURepository.GetAll().Where(q => q.isDel == false)
                                  join ck in _cangKuRepository.GetAll().Where(c => c.isDel == false) on qu.BM equals ck.CangkuNo
                                  select new CangKuQUWithCangKuDTO
                                  {
                                      CangKuId = qu.CangKuId,
                                      CangKuName = qu.CangKuName,
                                      BM = qu.BM,
                                      CangkuNo = ck.CangkuNo,
                                      CangName = ck.CangName,
                                      MShu = qu.MShu,
                                      Address = ck.Address,
                                      CreateName = qu.CreateName,
                                      CreateTime = qu.CreateTime,
                                      UpdateTime = qu.UpdateTime,
                                      CangNo=qu.CangNo,
                                  };

                if (!string.IsNullOrEmpty(request.CangkuNo))
                {
                    resultQuery = resultQuery.Where(q => q.CangkuNo.Contains(request.CangkuNo));
                }

                if (!string.IsNullOrEmpty(request.CangName))
                {
                    resultQuery = resultQuery.Where(q => q.CangName.Contains(request.CangName));
                }
                var totalCount = resultQuery.Count();
                var totalPages = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);
                
                // 分页查询
                var cangKuQUList = resultQuery
                    .OrderBy(q => q.CangKuId)
                    .Skip((request.PageIndex - 1) * request.PageSize)
                    .Take(request.PageSize)
                    .ToList();

                return Task.FromResult(new APIRequst<APIPageing<List<CangKuQUWithCangKuDTO>>>()
                {
                    Code = RequstCode.成功,
                    Msg = "查询成功",
                    Data = new APIPageing<List<CangKuQUWithCangKuDTO>>
                    {
                        Total = totalPages,
                        TotalCount = totalCount,
                        Data = cangKuQUList
                    }
                });
            }

        }
    }
